package com.cec.home.controller

import com.cec.home.service.impl.NewsServiceImpl
import com.m1905hd.common.util.Page
import com.mongodb.BasicDBObject
import com.mongodb.DBObject
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam

import java.text.SimpleDateFormat
/**
 * Created with IDEA
 * User:hxh
 * Date:16/4/22
 * Time:下午3:34
 */
@Controller
@RequestMapping("news")
class NewsController {

    @Autowired
    NewsServiceImpl newsService

    public static final SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat dateFormat2 = new SimpleDateFormat("MM.dd");

    @RequestMapping("list/{page}")
    def companyNewsList(Model model, @PathVariable int page, @RequestParam(defaultValue = "10") int size) {
        def query = new BasicDBObject()
        def news = newsService.list(query, [created: -1] as BasicDBObject, null, page, size) as Page
        news.data.each {
            it.date = dateFormat2.format(it.created)
        }
        model.addAttribute("page", news)
        model.addAttribute("list", news.data)
        return "newsList"
    }

    @RequestMapping("{id}")
    def companyNewsDetail(Model model, @PathVariable("id") int id) {
        def news = newsService.findOne(id) as DBObject, query = new BasicDBObject()
        news.date = dateFormat1.format(news.created)
        def newsList = newsService.getAll(query, [created: -1] as BasicDBObject, null)
        def lastNews = [index: -1] as BasicDBObject, nextNews = [index: -2] as BasicDBObject
        def list = new ArrayList()
        int index = 0
        for (int i = 0; i < newsList.size(); i++) {
            def map = new HashMap()
            map.put(i, newsList[i]._id)
            list.add(map)
            if (newsList[i]._id == news._id) {
                index = i
            }
        }
        list.each {
            def key = it.keySet()[0]
            if (key == index) {
                if (key != 0) {
                    lastNews.index = key - 1
                }
                if (key != list.size() - 1) {
                    nextNews.index = key + 1
                }
            }
        }
        if (lastNews.index != -1) {
            def last = newsService.findOne(list[lastNews.index].get(lastNews.index))
            last.index = lastNews.index
            lastNews = last
        }
        if (nextNews.index != -2) {
            def next = newsService.findOne(list[nextNews.index].get(nextNews.index))
            next.index = nextNews.index
            nextNews = next
        }
        model.addAttribute("news", news)
        model.addAttribute("lastNews", lastNews)
        model.addAttribute("nextNews", nextNews)
        return "newsDetail"
    }

}
